package com.nobuyme.box.toy;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.nobuyme.box.R;
import com.nobuyme.box.app.UrlCenter;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.framework.httprequest.HttpRequest;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;

/**
 * A simple {@link Fragment} subclass.
 */
public class UserHelpFragment extends BaseFragment {
    @Bind(R.id.webView_help)
    WebView webView;

    public UserHelpFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        initData();
        return view;
    }

    private void initData() {
        HttpRequest request = new HttpRequest(getActivity());
        request.get(UrlCenter.USER_HELP, null, new DataRequestListener<String>(String.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(String o) {
                super.onSuccess(o);
                try {
                    JSONObject jsonObject = new JSONObject(o);
                    String pic = (String) jsonObject.get("content");
                    webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                    webView.getSettings().setDefaultTextEncodingName("UTF-8");//设置默认为utf-8
                    webView.loadDataWithBaseURL(null, pic, "text/html", "utf-8", null);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_user_help, null);

        return inflate;
    }
}
